package conson.tech.manage.common.service;


import conson.tech.core.exception.BaseException;
import conson.tech.manage.common.constant.BusinessMessage;
import conson.tech.manage.common.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * ClassName: CommonUploadService
 * Description: 公共上传服务类
 * Author: HXL
 * Date: 2020/4/29 10:49 上午
 * Version: 1.0
 */
@Slf4j
@Service("commonUploadService")
public class CommonUploadService {
    @Value("${server_change_oss_ftp}")
    private String serverChange;
    @Autowired
    private OssFileService ossFileService;
    @Autowired
    private FileUploadService fileUploadService;

    public String commonUpload(String content,String path,String fileSuffixName){
        if (StringUtils.isEmpty(content)){
            return "";
        }
        BufferedOutputStream bos = null;
         FileOutputStream fos = null;
        String url = "";
        try {
            //String filePath = CommonUploadService.class.getClassLoader().getResource("").getFile();
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");
            if (StringUtils.isEmpty(fileSuffixName)){
                fileSuffixName = ".png";
            }else if (!fileSuffixName.startsWith(".")){
                fileSuffixName = "." + fileSuffixName;
            }
            String fileName = UUID.randomUUID().toString() +fileSuffixName;
            fileName = fileName.replace("-", "");
            File file = new File(fileName);
            if (!file.exists()){
                file.createNewFile();
            }
            byte[] bytes = Base64Util.decode(content);
             fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
            if (!"2".equals(serverChange)){
                url = fileUploadService.fileUpload(file, path + "/" + dateFormat1.format(new Date()));
            }else {
                url = ossFileService.fileUpload(file, path);
            }
            if (file.exists()){
                file.delete();
            }
        } catch (Exception e) {
            log.error("CommonUploadService --> commonUpload fail.",e);
            throw new BaseException(BusinessMessage.UPLOAD_FILE_ERROR);
        }finally {
            try {
                if (bos != null){
                    bos.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (IOException ex) {
                log.error("CommonUploadService --> commonUpload IO fail.",ex);
            }
        }
        return url;
    }

    public String commonUpload(MultipartFile file, String path){

        String url = "";
        try {
            //String filePath = CommonUploadService.class.getClassLoader().getResource("").getFile();
            //SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");

            if (!"2".equals(serverChange)){

                //url = fileUploadService.fileUpload(file, path + "/" + dateFormat1.format(new Date()));
                url = fileUploadService.fileUpload(file, path);
            }else {
                url = ossFileService.fileUploadSaveOldName(file, path);
            }
        } catch (Exception e) {
            log.error("CommonUploadService --> commonUpload fail.",e);
            throw new BaseException(BusinessMessage.UPLOAD_FILE_ERROR);
        }
        return url;
    }
}
